﻿// CWard.cpp: 实现文件
//

#include "pch.h"
#include "HospitalSystem.h"
#include "CWard.h"
#include "ward.h"
#include "patient.h"

// CWard

IMPLEMENT_DYNCREATE(CWard, CFormView)

CWard::CWard()
	: CFormView(IDD_CWard)
	, m_add1(_T(""))
	, m_add2(_T(""))
	, m_add3(_T(""))
	, m_xiugai1(_T(""))
	, m_xiugai2(_T(""))
	, m_xiugai3(_T(""))
	, m_delete(_T(""))
{

}

CWard::~CWard()
{
}

void CWard::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_wdlist);
	DDX_Text(pDX, IDC_EDIT2, m_add1);
	DDX_Text(pDX, IDC_EDIT1, m_add2);
	DDX_Text(pDX, IDC_EDIT3, m_add3);
	DDX_Text(pDX, IDC_EDIT8, m_xiugai1);
	DDX_Text(pDX, IDC_EDIT7, m_xiugai2);
	DDX_Text(pDX, IDC_EDIT9, m_xiugai3);
	//  DDX_Text(pDX, IDC_EDIT11, m_delete);
	DDX_Text(pDX, IDC_EDIT11, m_delete);
}

BEGIN_MESSAGE_MAP(CWard, CFormView)
	ON_BN_CLICKED(IDC_BUTTON5, &CWard::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CWard::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CWard::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON4, &CWard::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON1, &CWard::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CWard::OnBnClickedButton2)
END_MESSAGE_MAP()


// CWard 诊断

#ifdef _DEBUG
void CWard::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CWard::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CWard 消息处理程序


void CWard::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	CFormView::OnInitialUpdate();
	CString field[] = { _T("病房号"), _T("病房地址"), _T("医生编号") };
	for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
	{
		m_wdlist.InsertColumn(i, field[i], LVCFMT_CENTER, 90);
	}
	//设置正文
	CString strsql;
	strsql.Format(L"select * from ward");
	List_All(strsql);

}


void CWard::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	Ward m_cd;
	if (m_cd.IsOpen())
	{
		m_cd.Close();
	}
	m_cd.Open(CRecordset::dynaset);
	
	UpdateData(true);
	while (!m_cd.IsEOF())
	{
		
		if (m_add1 == m_cd.m_wd_id)
		{
			MessageBox(L"改病房已存在成功！");
			return;
		}
		m_cd.MoveNext();
	}
	m_cd.AddNew();
	m_cd.m_wd_id = m_add1;
	m_cd.m_wd_ad = m_add2;
	m_cd.m_dr_id = m_add3;
	if (m_cd.Update())
	{
		MessageBox(L"添加成功！");
		CString strsql;
		strsql.Format(L"select * from ward");
		List_All(strsql);
	}
	else
	{
		MessageBox(L"添加失败！");
	}
	m_cd.Close();
}


void CWard::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	m_add2.Empty();
	m_add1.Empty();
	m_add3.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CWard::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	Ward m_cd;
	m_cd.Open(CRecordset::dynaset);
	if (!m_cd.CanUpdate())
	{
		MessageBox(L"无权限！");
		return;
	}
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		m_cd.MoveNext();
		if (m_xiugai1 == m_cd.m_wd_id)
		{
			m_cd.Edit();
			m_cd.m_wd_ad = m_xiugai2;
			m_cd.m_dr_id = m_xiugai3;
			m_cd.Update();
			MessageBox(L"修改成功");
			CString strsql;
			strsql.Format(L"select * from ward");
			List_All(strsql);
			return;
		}
	}

	MessageBox(L"不存在改病房！");
}


void CWard::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	m_xiugai2.Empty();
	m_xiugai1.Empty();
	m_xiugai3.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CWard::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	Ward m_cd;
	Patient pt;
	m_cd.Open(CRecordset::dynaset);
	pt.Open(CRecordset::dynaset);
	UpdateData(true);
	m_cd.MoveFirst();
	pt.MoveFirst();
	while (!pt.IsEOF())
	{
		if (pt.m_wd_id == m_delete)
		{
			pt.Close();
			m_cd.Close();
			MessageBox(L"改病房还存在患者，不能删除！");
			return;
		}
		pt.MoveNext();
	}
	while (!m_cd.IsEOF())
	{
		if (m_cd.m_wd_id == m_delete)
		{
			m_cd.Delete();
			m_cd.Close();
			MessageBox(L"删除成功！");
			CString strsql;
			strsql.Format(L"select * from ward");
			List_All(strsql);
			return;
		}
		m_cd.MoveNext();
	}
	MessageBox(L"不存在病房，删除失败！");
}


void CWard::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	m_delete.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}
void CWard::List_All(CString str=L"select * from ward") {
	m_wdlist.DeleteAllItems();
	Ward m_wd;
	try {
		if (m_wd.IsOpen())
			m_wd.Close();
		if (!m_wd.Open(CRecordset::snapshot, str)) {
			MessageBox(L"打开数据库失败", L"数据库错误", MB_OK);
			return;
		}
	}
	catch (CDBException* e) {
		e->ReportError();
	}
	int nindex = 0;
	m_wd.MoveFirst();
	while (!m_wd.IsEOF()) {
		LV_ITEM litem;
		litem.mask = LVIF_TEXT;
		litem.iItem = nindex;
		litem.iSubItem = 0;
		litem.pszText = L"";
		m_wdlist.InsertItem(&litem);
		m_wdlist.SetItemText(nindex, 0, m_wd.m_wd_id);
		m_wdlist.SetItemText(nindex, 1, m_wd.m_wd_ad);
		m_wdlist.SetItemText(nindex, 2, m_wd.m_dr_id);
		m_wd.MoveNext();
		nindex++;
	}
	m_wd.Close();
}


